﻿using System;
using System.Windows.Forms;

namespace Medicalsystem.MCP.FormDesigner.Common
{
    /// <summary>
    /// 异常处理
    /// </summary>
    public static class TryCustomException
    {
        /// <summary>
        /// 异常处理
        /// </summary>
        /// <param name="ex"></param>
        public static void TransactException(Exception ex)
        {
            if (ex is CustomException)
            {
                CustomException customEx = (CustomException)ex;

                switch (customEx.ErrorType)
                {
                    case EErrorType.SIDValidateFailed:

                        //此处做SID验证失败后的相关处理

                        MessageBox.Show(customEx.ErrorText, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        break;

                    case EErrorType.Information:
                        MessageBox.Show(customEx.ErrorText, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        break;

                    case EErrorType.Warning:
                        MessageBox.Show(customEx.ErrorText, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        break;

                    case EErrorType.Exception:
                        MessageBox.Show(customEx.ErrorText, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        break;
                    default:
                        break;
                }
            }
            else
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }

    /// <summary>
    /// 自定义异常
    /// </summary>
    public class CustomException : Exception
    {
        private string _errorText = string.Empty;
        private EErrorType _errorType = EErrorType.None;

        public CustomException(EErrorType errorType, string errorText)
        {
            _errorText = errorText;
            _errorType = errorType;
        }
        public CustomException(EErrorType errorType, string errorText, Exception ex)
            : base(errorText, ex)
        {
            _errorText = errorText;
            _errorType = errorType;
        }
        public override string Message
        {
            get
            {
                return _errorText;
            }
        }

        /// <summary>
        /// 错误类型
        /// </summary>
        public EErrorType ErrorType
        {
            get
            {
                return _errorType;
            }
        }

        public string ErrorText
        {
            get
            {
                return _errorText;
            }
        }
    }
}